#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/7/11 8:02 下午
# @Author  : WangZhixing


# 1、将cluster中节点的label以及别的特征提取出来，并输入对应dataset中的label中。
# 2、把我们的mdg文件的node节点，改成数字。
# 3、把我们的node文件中的feature放到我们的dataset中的feature中。
# 4、获取节点的邻接权重矩阵
import pandas as pd
import json

import torch


class ProcessVector():
    # 初始化我们的dataset数据集
    def __init__(self):
        self.idx = 0
        self.dic = {}  # [节点idx：节点name]
        self.lst=[]# [[节点1,节点2,节点weight]]
        self.NodeLabel = {}  # [节点idx：节点label]
        self.NodeNameLabel={}


    def NewGroundTruth(self, cluster):
        with open(cluster, 'r') as f:
            label_idx = 1
            label = {}
            data = f.read().split('\n')[:-1]
            # 提取label转化为数字
            for line in data:
                x = line.split(' ')
                if x[1] not in label:
                    label[x[1]] = label_idx
                    label_idx += 1
                if x[2] in self.dic:
                    self.NodeLabel[self.dic[x[2]]] = label[x[1]]
                    self.NodeNameLabel[x[2]]=label[x[1]]


    def Node2Num(self, raw_path, edge_path,name_path,json_path):
        with open(json_path, "r") as jsonfile:
            datax = []
            vector = json.load(jsonfile)
            for node in vector.keys():
                self.dic[node]=self.idx
                self.NodeLabel[self.idx] = 0
                self.idx+=1
                datax.append(vector[node])
        datax= torch.tensor(datax, dtype=torch.float)

        with open(raw_path, 'r') as f:
            data = f.read().split('\n')[:-1]
            for line in data:
                x = line.split()
                s = x[1]
                t = x[2]
                if [self.dic[s], self.dic[t],1] not in self.lst:
                    self.lst.append([self.dic[s], self.dic[t],1])

            name = ['src', 'dst', 'value']
            test = pd.DataFrame(columns=name, data=self.lst)
            test.to_csv(edge_path,sep=" ",header=0,index=0)

            # name
            namepd = pd.DataFrame()
            name = list(self.dic.keys())
            idx = list(self.dic.values())
            namepd["idx"]=idx
            namepd["name"]=name
            namepd.to_csv(name_path,sep=" ",header=0,index=0)
        return datax